<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="代理/平台ID" prop="agentId">
        <el-input
          v-model="queryParams.agentId"
          placeholder="请输入代理/平台ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="agentName">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理等级" prop="agentLevel">
        <el-input
          v-model="queryParams.agentLevel"
          placeholder="请输入代理等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级代理" prop="superiorAgent">
        <el-input
          v-model="queryParams.superiorAgent"
          placeholder="请输入上级代理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分佣比例" prop="commissionRatio">
        <el-input
          v-model="queryParams.commissionRatio"
          placeholder="请输入分佣比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分佣金额" prop="commissionAmount">
        <el-input
          v-model="queryParams.commissionAmount"
          placeholder="请输入分佣金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安卓充值金额" prop="androidRechargeAmount">
        <el-input
          v-model="queryParams.androidRechargeAmount"
          placeholder="请输入安卓充值金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="iOS充值金额" prop="iosRechargeAmount">
        <el-input
          v-model="queryParams.iosRechargeAmount"
          placeholder="请输入iOS充值金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安卓注册数量" prop="androidRegistrationCount">
        <el-input
          v-model="queryParams.androidRegistrationCount"
          placeholder="请输入安卓注册数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="iOS注册数量" prop="iosRegistrationCount">
        <el-input
          v-model="queryParams.iosRegistrationCount"
          placeholder="请输入iOS注册数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员数量" prop="memberCount">
        <el-input
          v-model="queryParams.memberCount"
          placeholder="请输入会员数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值金额" prop="rechargeAmount">
        <el-input
          v-model="queryParams.rechargeAmount"
          placeholder="请输入充值金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日活用户数" prop="dailyActiveCount">
        <el-input
          v-model="queryParams.dailyActiveCount"
          placeholder="请输入日活用户数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推广用户数" prop="promotionUserCount">
        <el-input
          v-model="queryParams.promotionUserCount"
          placeholder="请输入推广用户数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安卓扣单数量" prop="androidOrderDeductionCount">
        <el-input
          v-model="queryParams.androidOrderDeductionCount"
          placeholder="请输入安卓扣单数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="iOS扣单数量" prop="iosOrderDeductionCount">
        <el-input
          v-model="queryParams.iosOrderDeductionCount"
          placeholder="请输入iOS扣单数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安卓扣单金额" prop="androidOrderDeductionAmount">
        <el-input
          v-model="queryParams.androidOrderDeductionAmount"
          placeholder="请输入安卓扣单金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="iOS扣单金额" prop="iosOrderDeductionAmount">
        <el-input
          v-model="queryParams.iosOrderDeductionAmount"
          placeholder="请输入iOS扣单金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安卓扣注册量" prop="androidRegistrationDeductionCount">
        <el-input
          v-model="queryParams.androidRegistrationDeductionCount"
          placeholder="请输入安卓扣注册量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="iOS扣注册量" prop="iosRegistrationDeductionCount">
        <el-input
          v-model="queryParams.iosRegistrationDeductionCount"
          placeholder="请输入iOS扣注册量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['consumer:statistics:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['consumer:statistics:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['consumer:statistics:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['consumer:statistics:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识" align="center" prop="id" v-if="true"/>
      <el-table-column label="代理/平台ID" align="center" prop="agentId" />
      <el-table-column label="用户昵称" align="center" prop="agentName" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="用户类型" align="center" prop="userType" />
      <el-table-column label="代理等级" align="center" prop="agentLevel" />
      <el-table-column label="上级代理" align="center" prop="superiorAgent" />
      <el-table-column label="分佣比例" align="center" prop="commissionRatio" />
      <el-table-column label="分佣金额" align="center" prop="commissionAmount" />
      <el-table-column label="安卓充值金额" align="center" prop="androidRechargeAmount" />
      <el-table-column label="iOS充值金额" align="center" prop="iosRechargeAmount" />
      <el-table-column label="安卓注册数量" align="center" prop="androidRegistrationCount" />
      <el-table-column label="iOS注册数量" align="center" prop="iosRegistrationCount" />
      <el-table-column label="会员数量" align="center" prop="memberCount" />
      <el-table-column label="充值金额" align="center" prop="rechargeAmount" />
      <el-table-column label="日活用户数" align="center" prop="dailyActiveCount" />
      <el-table-column label="推广用户数" align="center" prop="promotionUserCount" />
      <el-table-column label="安卓扣单数量" align="center" prop="androidOrderDeductionCount" />
      <el-table-column label="iOS扣单数量" align="center" prop="iosOrderDeductionCount" />
      <el-table-column label="安卓扣单金额" align="center" prop="androidOrderDeductionAmount" />
      <el-table-column label="iOS扣单金额" align="center" prop="iosOrderDeductionAmount" />
      <el-table-column label="安卓扣注册量" align="center" prop="androidRegistrationDeductionCount" />
      <el-table-column label="iOS扣注册量" align="center" prop="iosRegistrationDeductionCount" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['consumer:statistics:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['consumer:statistics:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代理/平台ID" prop="agentId">
          <el-input v-model="form.agentId" placeholder="请输入代理/平台ID" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="agentName">
          <el-input v-model="form.agentName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="代理等级" prop="agentLevel">
          <el-input v-model="form.agentLevel" placeholder="请输入代理等级" />
        </el-form-item>
        <el-form-item label="上级代理" prop="superiorAgent">
          <el-input v-model="form.superiorAgent" placeholder="请输入上级代理" />
        </el-form-item>
        <el-form-item label="分佣比例" prop="commissionRatio">
          <el-input v-model="form.commissionRatio" placeholder="请输入分佣比例" />
        </el-form-item>
        <el-form-item label="分佣金额" prop="commissionAmount">
          <el-input v-model="form.commissionAmount" placeholder="请输入分佣金额" />
        </el-form-item>
        <el-form-item label="安卓充值金额" prop="androidRechargeAmount">
          <el-input v-model="form.androidRechargeAmount" placeholder="请输入安卓充值金额" />
        </el-form-item>
        <el-form-item label="iOS充值金额" prop="iosRechargeAmount">
          <el-input v-model="form.iosRechargeAmount" placeholder="请输入iOS充值金额" />
        </el-form-item>
        <el-form-item label="安卓注册数量" prop="androidRegistrationCount">
          <el-input v-model="form.androidRegistrationCount" placeholder="请输入安卓注册数量" />
        </el-form-item>
        <el-form-item label="iOS注册数量" prop="iosRegistrationCount">
          <el-input v-model="form.iosRegistrationCount" placeholder="请输入iOS注册数量" />
        </el-form-item>
        <el-form-item label="会员数量" prop="memberCount">
          <el-input v-model="form.memberCount" placeholder="请输入会员数量" />
        </el-form-item>
        <el-form-item label="充值金额" prop="rechargeAmount">
          <el-input v-model="form.rechargeAmount" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="日活用户数" prop="dailyActiveCount">
          <el-input v-model="form.dailyActiveCount" placeholder="请输入日活用户数" />
        </el-form-item>
        <el-form-item label="推广用户数" prop="promotionUserCount">
          <el-input v-model="form.promotionUserCount" placeholder="请输入推广用户数" />
        </el-form-item>
        <el-form-item label="安卓扣单数量" prop="androidOrderDeductionCount">
          <el-input v-model="form.androidOrderDeductionCount" placeholder="请输入安卓扣单数量" />
        </el-form-item>
        <el-form-item label="iOS扣单数量" prop="iosOrderDeductionCount">
          <el-input v-model="form.iosOrderDeductionCount" placeholder="请输入iOS扣单数量" />
        </el-form-item>
        <el-form-item label="安卓扣单金额" prop="androidOrderDeductionAmount">
          <el-input v-model="form.androidOrderDeductionAmount" placeholder="请输入安卓扣单金额" />
        </el-form-item>
        <el-form-item label="iOS扣单金额" prop="iosOrderDeductionAmount">
          <el-input v-model="form.iosOrderDeductionAmount" placeholder="请输入iOS扣单金额" />
        </el-form-item>
        <el-form-item label="安卓扣注册量" prop="androidRegistrationDeductionCount">
          <el-input v-model="form.androidRegistrationDeductionCount" placeholder="请输入安卓扣注册量" />
        </el-form-item>
        <el-form-item label="iOS扣注册量" prop="iosRegistrationDeductionCount">
          <el-input v-model="form.iosRegistrationDeductionCount" placeholder="请输入iOS扣注册量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStatistics, getStatistics, delStatistics, addStatistics, updateStatistics } from "@/api/consumer/statistics";

export default {
  name: "Statistics",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户统计表格数据
      statisticsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentId: undefined,
        agentName: undefined,
        phone: undefined,
        userType: undefined,
        agentLevel: undefined,
        superiorAgent: undefined,
        commissionRatio: undefined,
        commissionAmount: undefined,
        androidRechargeAmount: undefined,
        iosRechargeAmount: undefined,
        androidRegistrationCount: undefined,
        iosRegistrationCount: undefined,
        memberCount: undefined,
        rechargeAmount: undefined,
        dailyActiveCount: undefined,
        promotionUserCount: undefined,
        androidOrderDeductionCount: undefined,
        iosOrderDeductionCount: undefined,
        androidOrderDeductionAmount: undefined,
        iosOrderDeductionAmount: undefined,
        androidRegistrationDeductionCount: undefined,
        iosRegistrationDeductionCount: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "唯一标识不能为空", trigger: "blur" }
        ],
        agentId: [
          { required: true, message: "代理/平台ID不能为空", trigger: "blur" }
        ],
        agentName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        userType: [
          { required: true, message: "用户类型不能为空", trigger: "change" }
        ],
        agentLevel: [
          { required: true, message: "代理等级不能为空", trigger: "blur" }
        ],
        superiorAgent: [
          { required: true, message: "上级代理不能为空", trigger: "blur" }
        ],
        commissionRatio: [
          { required: true, message: "分佣比例不能为空", trigger: "blur" }
        ],
        commissionAmount: [
          { required: true, message: "分佣金额不能为空", trigger: "blur" }
        ],
        androidRechargeAmount: [
          { required: true, message: "安卓充值金额不能为空", trigger: "blur" }
        ],
        iosRechargeAmount: [
          { required: true, message: "iOS充值金额不能为空", trigger: "blur" }
        ],
        androidRegistrationCount: [
          { required: true, message: "安卓注册数量不能为空", trigger: "blur" }
        ],
        iosRegistrationCount: [
          { required: true, message: "iOS注册数量不能为空", trigger: "blur" }
        ],
        memberCount: [
          { required: true, message: "会员数量不能为空", trigger: "blur" }
        ],
        rechargeAmount: [
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ],
        dailyActiveCount: [
          { required: true, message: "日活用户数不能为空", trigger: "blur" }
        ],
        promotionUserCount: [
          { required: true, message: "推广用户数不能为空", trigger: "blur" }
        ],
        androidOrderDeductionCount: [
          { required: true, message: "安卓扣单数量不能为空", trigger: "blur" }
        ],
        iosOrderDeductionCount: [
          { required: true, message: "iOS扣单数量不能为空", trigger: "blur" }
        ],
        androidOrderDeductionAmount: [
          { required: true, message: "安卓扣单金额不能为空", trigger: "blur" }
        ],
        iosOrderDeductionAmount: [
          { required: true, message: "iOS扣单金额不能为空", trigger: "blur" }
        ],
        androidRegistrationDeductionCount: [
          { required: true, message: "安卓扣注册量不能为空", trigger: "blur" }
        ],
        iosRegistrationDeductionCount: [
          { required: true, message: "iOS扣注册量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户统计列表 */
    getList() {
      this.loading = true;
      listStatistics(this.queryParams).then(response => {
        this.statisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        agentId: undefined,
        agentName: undefined,
        phone: undefined,
        userType: undefined,
        agentLevel: undefined,
        superiorAgent: undefined,
        commissionRatio: undefined,
        commissionAmount: undefined,
        androidRechargeAmount: undefined,
        iosRechargeAmount: undefined,
        androidRegistrationCount: undefined,
        iosRegistrationCount: undefined,
        memberCount: undefined,
        rechargeAmount: undefined,
        dailyActiveCount: undefined,
        promotionUserCount: undefined,
        androidOrderDeductionCount: undefined,
        iosOrderDeductionCount: undefined,
        androidOrderDeductionAmount: undefined,
        iosOrderDeductionAmount: undefined,
        androidRegistrationDeductionCount: undefined,
        iosRegistrationDeductionCount: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getStatistics(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改用户统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateStatistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addStatistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户统计编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delStatistics(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consumer/statistics/export', {
        ...this.queryParams
      }, `statistics_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
